package com.example.yyjgit.Controller;

import com.example.yyjgit.Entity.Relics;
import com.example.yyjgit.Entity.ResultEntity;
import com.example.yyjgit.Service.RelicsService;
import com.example.yyjgit.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/Relics/")
public class RelicsController {
    @Resource
    RelicsService relicsService;
    ResultEntity resultEntity=null;

    //添加文物
    @PutMapping("addRelics")
    private String addRelics (@RequestParam("Name")String Name, @RequestParam("Img") String Img , @RequestParam("Era")String Era, @RequestParam("State")String State){
        resultEntity=new ResultEntity();
        try {
            if (relicsService.addRelics(Name, Img,Era,State) > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    //删除文物
    @DeleteMapping("delRelics")
    private String delExhibitById (@RequestParam("id") int id){
        resultEntity =new ResultEntity();
        try {
            if (relicsService.delRelics(id)>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功");
            }else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }
        return  JsonUtil.getJsonTool().toJson(resultEntity);
    }
    //修改文物
    @PutMapping("upRelics")
    private String upRelics(@RequestParam("Name")String Name,@RequestParam("Img") String Img ,@RequestParam("Era")String Era,@RequestParam("State")String State,@RequestParam("id") int id){
        resultEntity=new ResultEntity();
        try{

            if(relicsService.upRelics(Name,Img,Era,State,id) > 0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功");
            }else {
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    //查询文物
    @GetMapping("getExhibit")
    private String getRelicsById(@RequestParam("id")int id){
        resultEntity=new ResultEntity();
        try{
            Relics relics=relicsService.getRelicsById(id);
            if(relics==null){
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            }else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(relics);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

}
